package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.JoinMessage;
import org.itboys.mobile.service.mongo.site.JoinSiteMessageService;
import org.itboys.mobile.service.mongo.site.JoinSiteValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/9/22
 * 描述：雄安加盟网_留言
 */
@Controller
@RequestMapping("/join/site/message")
public class JoinSiteMessageController extends BaseController{
    @Autowired
    private JoinSiteMessageService messageService;
    @Autowired
    private JoinSiteValidateCodeService codeService;

    /**
     * 用户留言
     * @param request
     * @param response
     * @param name
     * @param phone
     * @param sex
     * @param content
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="productId",required=true)Long productId,
                     @RequestParam(value="name",required=true)String name,
                     @RequestParam(value="phone",required=true)String phone,
                     @RequestParam(value="sex",required=true)String sex,
                     @RequestParam(value="content",required=true)String content,
                     @RequestParam(value="code",required=true)String code,
                     @RequestParam(value="provinceId",required=false)Long provinceId,
                     @RequestParam(value="provinceName",required=false)String provinceName,
                     @RequestParam(value="cityId",required=false)Long cityId,
                     @RequestParam(value="cityName",required=false)String cityName,
                     @RequestParam(value="districtId",required=false)Long districtId,
                     @RequestParam(value="districtName",required=false)String districtName
                     ){
        try {
            //codeService.checkValidateCode(phone,code);
            messageService.save(productId,name,sex,phone,code,content,provinceId,provinceName,cityId,cityName,
                    districtId,districtName);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/message/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }



    /**
     * 逐个删除某个留言
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            messageService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/message/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 批量删除
     * @param request
     * @param response
     * @param ids
     */
    @RequestMapping("/batchDelete")
    public void batchDelete(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value="ids",required=true)List<Long> ids
    ){
        try {
            messageService.batchDelete(ids);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/reply/batchDelete------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }
}
